
package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import tools.Parametres;

public class FenetreOption extends JFrame{
    
    private JPanel content;
    private JButton ok;
    private JButton cancel;
    private JTextField folderURL;
    private JTextField port;
    private FenetrePrincipale fenetrePrincipale;
    
    public FenetreOption(FenetrePrincipale fenetrePrincipale){
        
        this.content = new JPanel();
        this.ok = new JButton("OK");
        this.cancel = new JButton("Annuler");
        JLabel folder = new JLabel("Emplacement : ", SwingConstants.RIGHT);
        this.folderURL = new JTextField(19);
        JButton browse = new JButton("...");
        this.port = new JTextField(21);
        Parametres p = new Parametres();
        this.port.setText(""+p.getPort());
        
        
        this.fenetrePrincipale = fenetrePrincipale;
        
        this.folderURL.setText(p.defaultFolder);
        
        browse.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                JFileChooser fc = new JFileChooser();
                fc.setDialogTitle("Parcourir..");
                fc.setApproveButtonText("Ouvrir");
                fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int returnVal = fc.showOpenDialog(null);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File choosen = fc.getSelectedFile();
                    FenetreOption.this.folderURL.setText(choosen.getPath());
                }
            }
        });
        
        ok.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                Parametres p = new Parametres();
                String newPath = folderURL.getText();
                int newPort = Integer.parseInt(port.getText());
                System.out.println(newPort);
                
                FenetreOption.this.fenetrePrincipale.getOption().setEnabled(true);
                
            }
        });
        cancel.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e){
                FenetreOption.this.fenetrePrincipale.getOption().setEnabled(true);
                dispose();
                
            }
        });
        this.content.setLayout(new BorderLayout());
        JPanel panleft = new JPanel(new GridLayout(4,1));
        panleft.add(folder);
        panleft.add(new JLabel("Port : ", SwingConstants.RIGHT));
        
        JPanel pancent = new JPanel(new GridLayout(4,1));
        JPanel pan1= new JPanel();
        pan1.add(folderURL);
        pan1.add(browse);
        pancent.add(pan1);
        JPanel pan2 = new JPanel();
        pan2.add(port);
        pancent.add(pan2);
        JPanel pansouth = new JPanel();
        pansouth.add(ok);
        pansouth.add(cancel);
        this.content.add(panleft, BorderLayout.WEST);
        this.content.add(pancent, BorderLayout.CENTER);
        this.content.add(pansouth, BorderLayout.SOUTH);
        
        this.add(this.content);
        this.setSize(450, 300);
        this.setTitle("Option");
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.setVisible(true);
    }
    
}
